package com.lkd.service.impl;

import com.lkd.entity.Task;
import com.lkd.mapper.StatisticsMapper;
import com.lkd.service.StatisticsService;
import com.lkd.vo.TaskReportInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public Integer orderCount(LocalDateTime start, LocalDateTime end) {
        return statisticsMapper.orderCount(start, end);
    }

    @Override
    public Double orderAmount(LocalDateTime start, LocalDateTime end) {
        return statisticsMapper.orderAmount(start, end);
    }

    @Override
    public Integer nodeCount() {
        return statisticsMapper.nodeCount();
    }

    @Override
    public Integer partnerCount() {
        return statisticsMapper.partnerCount();
    }

    @Override
    public List<TaskReportInfoVo> taskReportInfo(LocalDateTime start, LocalDateTime end) {
        //查询所有的工单
        List<Task> taskList = statisticsMapper.findTask(start, end);

        //运营工单统计
        TaskReportInfoVo taskReportInfoVo1 = new TaskReportInfoVo();
        taskReportInfoVo1.setTotal(taskList.stream().filter(task -> task.getProductTypeId() == 2).count());//总数量
        taskReportInfoVo1.setCompletedTotal(taskList.stream().filter(task -> task.getProductTypeId() == 2 && task.getTaskStatus() == 4).count());//完成
        taskReportInfoVo1.setCancelTotal(taskList.stream().filter(task -> task.getProductTypeId() == 2 && task.getTaskStatus() == 3).count());//取消
        taskReportInfoVo1.setProgressTotal(taskList.stream().filter(task -> task.getProductTypeId() == 2 && task.getTaskStatus() == 2).count());//进行
        taskReportInfoVo1.setWorkerCount(0L);//todo
        taskReportInfoVo1.setDate(new Date().toString());//todo
        taskReportInfoVo1.setRepair(false);//是否是运维工单

        //运维工单统计
        TaskReportInfoVo taskReportInfoVo2 = new TaskReportInfoVo();
        taskReportInfoVo2.setTotal(taskList.stream().filter(task -> task.getProductTypeId() != 2).count());//总数量
        taskReportInfoVo2.setCompletedTotal(taskList.stream().filter(task -> task.getProductTypeId() != 2 && task.getTaskStatus() == 4).count());//完成
        taskReportInfoVo2.setCancelTotal(taskList.stream().filter(task -> task.getProductTypeId() != 2 && task.getTaskStatus() == 3).count());//取消
        taskReportInfoVo2.setProgressTotal(taskList.stream().filter(task -> task.getProductTypeId() != 2 && task.getTaskStatus() == 2).count());//进行
        taskReportInfoVo2.setWorkerCount(0L);//todo
        taskReportInfoVo2.setDate(new Date().toString());//todo
        taskReportInfoVo2.setRepair(true);//是否是运维工单

        List<TaskReportInfoVo> list = new ArrayList<>();
        list.add(taskReportInfoVo1);
        list.add(taskReportInfoVo2);
        return list;
    }
}
